
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>The redirects app &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="The sitemap framework" href="sitemaps.html" />
    <link rel="prev" title="Validators" href="postgres/validators.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="postgres/validators.html" title="Validators">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="sitemaps.html" title="The sitemap framework">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-redirects">
            
  <div class="section" id="s-module-django.contrib.redirects">
<span id="s-the-redirects-app"></span><span id="module-django.contrib.redirects"></span><span id="the-redirects-app"></span><h1>The redirects app<a class="headerlink" href="#module-django.contrib.redirects" title="Permalink to this headline">¶</a></h1>
<p>Django comes with an optional redirects application. It lets you store simple
redirects in a database and handles the redirecting for you. It uses the HTTP
response status code <code class="docutils literal notranslate"><span class="pre">301</span> <span class="pre">Moved</span> <span class="pre">Permanently</span></code> by default.</p>
<div class="section" id="s-installation">
<span id="installation"></span><h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>To install the redirects app, follow these steps:</p>
<ol class="arabic simple">
<li>Ensure that the <code class="docutils literal notranslate"><span class="pre">django.contrib.sites</span></code> framework
<a class="reference internal" href="sites.html#enabling-the-sites-framework"><span class="std std-ref">is installed</span></a>.</li>
<li>Add <code class="docutils literal notranslate"><span class="pre">'django.contrib.redirects'</span></code> to your <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> setting.</li>
<li>Add <code class="docutils literal notranslate"><span class="pre">'django.contrib.redirects.middleware.RedirectFallbackMiddleware'</span></code>
to your <a class="reference internal" href="../settings.html#std:setting-MIDDLEWARE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MIDDLEWARE</span></code></a> setting.</li>
<li>Run the command <a class="reference internal" href="../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">manage.py</span> <span class="pre">migrate</span></code></a>.</li>
</ol>
</div>
<div class="section" id="s-how-it-works">
<span id="how-it-works"></span><h2>How it works<a class="headerlink" href="#how-it-works" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">manage.py</span> <span class="pre">migrate</span></code> creates a <code class="docutils literal notranslate"><span class="pre">django_redirect</span></code> table in your database. This
is a simple lookup table with <code class="docutils literal notranslate"><span class="pre">site_id</span></code>, <code class="docutils literal notranslate"><span class="pre">old_path</span></code> and <code class="docutils literal notranslate"><span class="pre">new_path</span></code> fields.</p>
<p>The <a class="reference internal" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware" title="django.contrib.redirects.middleware.RedirectFallbackMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">RedirectFallbackMiddleware</span></code></a>
does all of the work. Each time any Django application raises a 404
error, this middleware checks the redirects database for the requested
URL as a last resort. Specifically, it checks for a redirect with the
given <code class="docutils literal notranslate"><span class="pre">old_path</span></code> with a site ID that corresponds to the
<a class="reference internal" href="../settings.html#std:setting-SITE_ID"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SITE_ID</span></code></a> setting.</p>
<ul class="simple">
<li>If it finds a match, and <code class="docutils literal notranslate"><span class="pre">new_path</span></code> is not empty, it redirects to
<code class="docutils literal notranslate"><span class="pre">new_path</span></code> using a 301 (“Moved Permanently”) redirect. You can subclass
<a class="reference internal" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware" title="django.contrib.redirects.middleware.RedirectFallbackMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">RedirectFallbackMiddleware</span></code></a>
and set
<a class="reference internal" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class" title="django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class"><code class="xref py py-attr docutils literal notranslate"><span class="pre">response_redirect_class</span></code></a>
to <a class="reference internal" href="../request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.http.HttpResponseRedirect</span></code></a> to use a
<code class="docutils literal notranslate"><span class="pre">302</span> <span class="pre">Moved</span> <span class="pre">Temporarily</span></code> redirect instead.</li>
<li>If it finds a match, and <code class="docutils literal notranslate"><span class="pre">new_path</span></code> is empty, it sends a 410 (“Gone”)
HTTP header and empty (content-less) response.</li>
<li>If it doesn’t find a match, the request continues to be processed as
usual.</li>
</ul>
<p>The middleware only gets activated for 404s – not for 500s or responses of any
other status code.</p>
<p>Note that the order of <a class="reference internal" href="../settings.html#std:setting-MIDDLEWARE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MIDDLEWARE</span></code></a> matters. Generally, you can put
<a class="reference internal" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware" title="django.contrib.redirects.middleware.RedirectFallbackMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">RedirectFallbackMiddleware</span></code></a> at the
end of the list, because it’s a last resort.</p>
<p>For more on middleware, read the <a class="reference internal" href="../../topics/http/middleware.html"><span class="doc">middleware docs</span></a>.</p>
</div>
<div class="section" id="s-how-to-add-change-and-delete-redirects">
<span id="how-to-add-change-and-delete-redirects"></span><h2>How to add, change and delete redirects<a class="headerlink" href="#how-to-add-change-and-delete-redirects" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-via-the-admin-interface">
<span id="via-the-admin-interface"></span><h3>Via the admin interface<a class="headerlink" href="#via-the-admin-interface" title="Permalink to this headline">¶</a></h3>
<p>If you’ve activated the automatic Django admin interface, you should see a
“Redirects” section on the admin index page. Edit redirects as you edit any
other object in the system.</p>
</div>
<div class="section" id="s-via-the-python-api">
<span id="via-the-python-api"></span><h3>Via the Python API<a class="headerlink" href="#via-the-python-api" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.contrib.redirects.models.Redirect">
<em class="property">class </em><code class="descclassname">models.</code><code class="descname">Redirect</code><a class="headerlink" href="#django.contrib.redirects.models.Redirect" title="Permalink to this definition">¶</a></dt>
<dd><p>Redirects are represented by a standard <a class="reference internal" href="../../topics/db/models.html"><span class="doc">Django model</span></a>,
which lives in <a class="reference external" href="https://github.com/django/django/blob/stable/2.2.x/django/contrib/redirects/models.py">django/contrib/redirects/models.py</a>. You can access
redirect objects via the <a class="reference internal" href="../../topics/db/queries.html"><span class="doc">Django database API</span></a>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-middleware">
<span id="middleware"></span><h2>Middleware<a class="headerlink" href="#middleware" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.contrib.redirects.middleware.RedirectFallbackMiddleware">
<em class="property">class </em><code class="descclassname">middleware.</code><code class="descname">RedirectFallbackMiddleware</code><a class="headerlink" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware" title="Permalink to this definition">¶</a></dt>
<dd><p>You can change the <a class="reference internal" href="../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> classes used
by the middleware by creating a subclass of
<a class="reference internal" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware" title="django.contrib.redirects.middleware.RedirectFallbackMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">RedirectFallbackMiddleware</span></code></a>
and overriding <code class="docutils literal notranslate"><span class="pre">response_gone_class</span></code> and/or <code class="docutils literal notranslate"><span class="pre">response_redirect_class</span></code>.</p>
<dl class="attribute">
<dt id="django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_gone_class">
<code class="descname">response_gone_class</code><a class="headerlink" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_gone_class" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> class used when a
<a class="reference internal" href="#django.contrib.redirects.models.Redirect" title="django.contrib.redirects.models.Redirect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Redirect</span></code></a> is not found for the
requested path or has a blank <code class="docutils literal notranslate"><span class="pre">new_path</span></code> value.</p>
<p>Defaults to <a class="reference internal" href="../request-response.html#django.http.HttpResponseGone" title="django.http.HttpResponseGone"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponseGone</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class">
<code class="descname">response_redirect_class</code><a class="headerlink" href="#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> class that handles the redirect.</p>
<p>Defaults to <a class="reference internal" href="../request-response.html#django.http.HttpResponsePermanentRedirect" title="django.http.HttpResponsePermanentRedirect"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponsePermanentRedirect</span></code></a>.</p>
</dd></dl>

</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The redirects app</a><ul>
<li><a class="reference internal" href="#installation">Installation</a></li>
<li><a class="reference internal" href="#how-it-works">How it works</a></li>
<li><a class="reference internal" href="#how-to-add-change-and-delete-redirects">How to add, change and delete redirects</a><ul>
<li><a class="reference internal" href="#via-the-admin-interface">Via the admin interface</a></li>
<li><a class="reference internal" href="#via-the-python-api">Via the Python API</a></li>
</ul>
</li>
<li><a class="reference internal" href="#middleware">Middleware</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="postgres/validators.html"
                        title="previous chapter">Validators</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="sitemaps.html"
                        title="next chapter">The sitemap framework</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/contrib/redirects.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="postgres/validators.html" title="Validators">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="sitemaps.html" title="The sitemap framework">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>